//
//  HLIMKitCellLayoutConfig.m
//  YueYue
//
//  Created by hulu001 on 2024/1/11.
//

#import "HLIMKitCellLayoutConfig.h"
#import "HLIMKitMessageModel.h"
#import "HLIMKitSessionContentConfigFactory.h"
#import "HLChatKit.h"

@implementation HLIMKitCellLayoutConfig


- (CGSize)contentSize:(HLIMKitMessageModel *)model cellWidth:(CGFloat)width
{
    id<HLIMSessionContentConfig>config = [HLIMKitSessionContentConfigFactory.sharedFacotry configBy:model.message];
    return [config contentSize:width message:model.message];
}

/// 需要构造的cellContent类名
- (NSString *)cellContent:(HLIMKitMessageModel *)model
{
    id<HLIMSessionContentConfig>config = [HLIMKitSessionContentConfigFactory.sharedFacotry configBy:model.message];
    return [config cellContent:model.message];
}

/// 左对齐的气泡，cell内容距离气泡的内间距
- (UIEdgeInsets)contentViewInsets:(HLIMKitMessageModel *)model
{
    id<HLIMSessionContentConfig>config = [HLIMKitSessionContentConfigFactory.sharedFacotry configBy:model.message];
    return [config contentViewInsets:model.message];
}

/// 左对齐的气泡，cell气泡距离整个cell的内间距
- (UIEdgeInsets)cellInsets:(HLIMKitMessageModel *)model
{
    if ([[self cellContent:model] isEqualToString:@"HLIMKitMessageTipContentView"]) {
        return UIEdgeInsetsMake(10, 0, 10, 0);
    }
    
    // 要不要显示昵称 气泡位置分别设置
    if ([self shouldShowAvatar:model])
    {
        return UIEdgeInsetsMake(4, 54, 16, 56);
    }
    else
    {
        return UIEdgeInsetsMake(4, 54, 16, 12);
    }
}

- (UIEdgeInsets)replyContentViewInsets:(HLIMKitMessageModel *)model{
    id<HLIMSessionContentConfig>config = [[HLIMKitSessionContentConfigFactory sharedFacotry] replyConfigBy:model.repliedMessage];
    return [config contentViewInsets:model.repliedMessage];
}


- (UIEdgeInsets)replyCellInsets:(HLIMKitMessageModel *)model
{
    if ([[self cellContent:model] isEqualToString:@"HLIMKitMessageUndefineContentView"]) {
        return UIEdgeInsetsZero;
    }
    CGFloat cellTopToBubbleTop           = 3;
    CGFloat otherNickNameHeight          = 20;
    CGFloat bubbleLeftToCellLeft         = 13;
    CGFloat otherBubbleOriginX           = [self shouldShowAvatar:model] ? [self avatarSize:model].width + bubbleLeftToCellLeft : 18;
    CGFloat cellBubbleButtomToCellButtom = 1;
    
    return UIEdgeInsetsMake(cellTopToBubbleTop + otherNickNameHeight ,otherBubbleOriginX,cellBubbleButtomToCellButtom, 0);
}

- (CGSize)replyContentSize:(HLIMKitMessageModel *)model cellWidth:(CGFloat)cellWidth {
    id<HLIMSessionContentConfig>config = [[HLIMKitSessionContentConfigFactory sharedFacotry] replyConfigBy:model.repliedMessage];
    return [config contentSize:cellWidth message:model.repliedMessage];
}

- (NSString *)replyContent:(HLIMKitMessageModel *)model {
    id<HLIMSessionContentConfig>config = [[HLIMKitSessionContentConfigFactory sharedFacotry] replyConfigBy:model.repliedMessage];
    NSString *cellContent = [config cellContent:model.repliedMessage];
    return cellContent.length ? cellContent : @"HLIMKitMessageUndefineContentView";
}

/// 左对齐的气泡，头像控件的 size
- (CGSize)avatarSize:(HLIMKitMessageModel *)model
{
    if (model.message.messageType == 100) {
        NIMCustomObject *object = (NIMCustomObject *)model.message.messageObject;
        
        if ([object.attachment isKindOfClass:NSClassFromString(@"HLPushAttachment")]) {
            return CGSizeMake(44, 44);
        }
    }
    return CGSizeMake(36, 36);
}

/// 左对齐的气泡，头像控件的 origin 点
- (CGPoint)avatarMargin:(HLIMKitMessageModel *)model
{
    if (model.message.messageType == 100) {
        NIMCustomObject *object = (NIMCustomObject *)model.message.messageObject;
        
        if ([object.attachment isKindOfClass:NSClassFromString(@"HLPushAttachment")]) {
            return CGPointMake(12, 8);
        }
    }
    return CGPointMake(12, 0);
}

/// 左对齐的气泡，昵称控件的 origin 点
- (CGPoint)nickNameMargin:(HLIMKitMessageModel *)model
{
    return CGPointMake(16, 40 + 8);
}

/// 消息显示在左边
- (BOOL)shouldShowLeft:(HLIMKitMessageModel *)model
{
    return !model.message.isOutgoingMsg;
}

- (BOOL)shouldShowAvatar:(HLIMKitMessageModel *)model
{
    if (model.message.messageType == NIMMessageTypeTip) {
        return NO;
    } else {
        return [HLChatKit.shareSDK.config setting:model.message].showAvatar;
    }
}

- (BOOL)shouldShowNick:(HLIMKitMessageModel *)model
{
    if (model.message.messageType == NIMMessageTypeTip || model.message.messageType == NIMMessageTypeNotification) {
        return NO;
    }
    
    // 具体私聊或者群聊 可以再判断
    BOOL isTeam = NO;
//    return isTeam; // 群聊显示昵称
    return (!model.message.isOutgoingMsg && isTeam); // 群聊 && 别人的消息 显示昵称
    
    return YES;
}

- (BOOL)shouldShowTime:(HLIMKitMessageModel *)model
{
    return [HLChatKit.shareSDK.config setting:model.message].showTime;
}

- (BOOL)disableRetryButton:(nonnull HLIMKitMessageModel *)model {
    
    if (model.message.isOutgoingMsg) {
        NSString *yindun = model.message.localExt[@"yidun"];
        if (yindun) {
            return NO;
        }
        return model.message.deliveryState != NIMMessageDeliveryStateFailed;
    } else {
        return model.message.attachmentDownloadState != NIMMessageAttachmentDownloadStateFailed;
    }
//
//    if (!model.message.isReceivedMsg)
//    {
//        return model.message.deliveryState != NIMMessageDeliveryStateFailed;
//    }
//    else
//    {
//        return model.message.attachmentDownloadState != NIMMessageAttachmentDownloadStateFailed;
//    }
}

- (BOOL)shouldShowUnread:(HLIMKitMessageModel *)model
{
    BOOL isTeam = model.message.session.sessionType == NIMSessionTypeTeam;
    
    if (isTeam) {
        return NO;
    } else {
        if (model.message.isOutgoingMsg) return YES;
        return NO;
    }
}

@end
